package com.yf.dmp.controller;

import com.alibaba.fastjson.JSON;
import com.yf.dmp.bean.QueryInfo;
import com.yf.dmp.bean.User;
import com.yf.dmp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    UserService uService;

    @RequestMapping("/user")
    public String getUser(QueryInfo queryInfo){
        return uService.getUser(queryInfo);
    }

    @RequestMapping("/updateuserstate")
    public String updateUserState(Integer id,boolean state){
        int i = uService.updateUserState (id, state);
        return  i>0?"Success":"Error";
    }

    @RequestMapping("/updatepassword")
    public String updatePassword(Integer id,String password,String newpassword){
        int i = uService.updatePassword (id, password,newpassword);
        return  i>0?"Success":"Error";
    }
    @RequestMapping("/adduser")
    public String addUser(@RequestBody User user){
        int i = uService.addUser(user);
        return   i>0?"Success":"Error";
    }

    @RequestMapping("/deleteuser")
    public String deleteUserById(int id){
        int i = uService.deleteUserById(id);
        return   i>0?"Success":"Error";
    }

    @RequestMapping("/getupdateuser")
    public String getUpdateUser(int id){
        System.out.println(id+"用户进入修改");
        User user = uService.getUpdateUser(id);
        String jsonstr = JSON.toJSONString(user);
        return jsonstr;
    }

    @RequestMapping("/updateuser")
    public String updateUser(@RequestBody User user){
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        int i = uService.updateUser(user);
        return   i>0?"Success":"Error";
    }
}
